<?php

class TimesheetsController extends AppController {

    var $name = 'Timesheets';
    var $helpers = array('Javascript', 'Ajax', 'Time', 'Number');

    function index() {
        $this->Timesheet->recursive = 0;
        $this->set('closeModalbox', false);
        // $duration = $this->Timesheet->query("SELECT subtime(timesheets.`finish time`, timesheets.`start time`) FROM timesheets");
        //	$this->set('duration', $duration[0][0]['subtime(timesheets.`finish time`, timesheets.`start time`)']);
        $this->set('timesheets', $this->paginate());
        $this->set('closeModalbox', true);
    }

    function view($id = null) {
        if (!$id) {
            $this->set('closeModalbox', false);
            $this->Session->setFlash(__('Invalid timesheet', true));
            $this->redirect($this->referer());
        }
        $this->set('timesheet', $this->Timesheet->read(null, $id));
        $this->set('closeModalbox', true);
    }

    function add() {
        $this->set('closeModalbox', false);
        if (!empty($this->data)) {
            $this->Timesheet->create();
            if ($this->Timesheet->save($this->data)) {
                $this->Session->setFlash(__('The timesheet has been saved', true));
                $this->redirect(array('controller' => 'clients', 'action' => 'view/'.$this->data['Timesheet']['client_id']));
                $this->set('closeModalbox', true);
            } else {
                $this->Session->setFlash(__('The timesheet could not be saved. Please, try again.', true));
            $this->redirect($this->referer());
            }
        }
        $clients = $this->Timesheet->Client->find('list');
        $users = $this->Timesheet->User->find('list');
        $this->set(compact('clients', 'users'));
    }

    function edit($id = null) {
        $this->set('closeModalbox', false);
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid timesheet', true));
            $this->redirect($this->referer());
        }
        if (!empty($this->data)) {
            if ($this->Timesheet->save($this->data)) {
                $this->Session->setFlash(__('The timesheet has been saved', true));
                 $this->redirect(array('controller' => 'clients', 'action' => 'view/'.$this->data['Timesheet']['client_id']));
                $this->set('closeModalbox', true);
            } else {
                $this->Session->setFlash(__('The timesheet could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Timesheet->read(null, $id);
        }
        $clients = $this->Timesheet->Client->find('list');
        $users = $this->Timesheet->User->find('list');
        $this->set(compact('clients', 'users'));
    }

    function delete($id = null) {
        $this->set('closeModalbox', false);
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for timesheet', true));
           $this->redirect($this->referer());
        }
        if ($this->Timesheet->delete($id)) {
            $this->Session->setFlash(__('Timesheet deleted', true));
            $this->redirect($this->referer());
        }
        $this->Session->setFlash(__('Timesheet was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function search() {
        $this->set('closeModalbox', false);
        // the page we will redirect to
        $url['action'] = 'searchIndex';

        // build a URL will all the search elements in it
        // the resulting URL will be 
        // example.com/cake/posts/index/Search.keywords:mykeyword/Search.tag_id:3
        foreach ($this->data as $k => $v) {
            foreach ($v as $kk => $vv) {
                $url[$k . '.' . $kk] = $vv['year'] . '-' . $vv['month'] . '-' . $vv['day'];
            }
        }
        // redirect the user to the url
        $this->redirect($url, null, true);
    }

    function searchIndex() {
        $this->set('closeModalbox', false);
        $this->Timesheet->recursive = 0;
        $this->paginate = array('conditions' => array('date BETWEEN ? AND ?' => array($this->passedArgs['Timesheet.startDate'], $this->passedArgs['Timesheet.endDate'])), 'limit' => 20);
        $this->set('timesheets', $this->paginate());
    }

    function getCountNotCompletedTimesheets() {
        $this->set('closeModalbox', false);
        $timesheets = $this->Timesheet->query("SELECT COUNT(*) FROM timesheets WHERE status = '0'");
        return $timesheets[0][0]['COUNT(*)'];
    }

    function viewStatus() {
        $this->set('closeModalbox', false);
        $this->Timesheet->recursive = 0;
        $this->paginate = array('conditions' => array('timesheet.status' => '0'), 'limit' => 10);
        $this->set('timesheets', $this->paginate());
    }

    function beforeFilter() {
        $this->set('closeModalbox', false);
        parent::beforeFilter();
        $this->Auth->allowedActions = array('index', 'view');
    }

    function latestTimesheets($id = null){
        if ($id == null) {
            echo "No id passed";
        } else {
            $results = $this->Timesheet->query("SELECT * FROM  timesheets where user_id =".$id." ORDER BY date LIMIT 5");
            return $results;
        }
    }
}

?>